﻿using System;
using System.Collections.Generic;
using System.Linq;
using Crosland.Automotriz.Entidad;
using System.Collections;

namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsTD_UbigeoDAO
    {
        private readonly DM_CROSLAND_20130417Entities ContextoDeObjs = new DM_CROSLAND_20130417Entities();

        public List<TD_UBIGEO> ListarDepartamento()
        {
            List<TD_UBIGEO> lista = new List<TD_UBIGEO>();
            try
            {

                var results = from c in ContextoDeObjs.TD_UBIGEO
                              where c.COD_DEPARTAMENTO != "000000" && c.COD_DEPARTAMENTO != "NN"
                              group c by new { c.COD_DEPARTAMENTO,c.DES_DEPARTAMENTO } into p
                              orderby p.Key.DES_DEPARTAMENTO ascending
                              select new
                              {
                                  Codigo = p.Key.COD_DEPARTAMENTO,
                                  Descripcion = p.Key.DES_DEPARTAMENTO
                              };
                var res = results.Distinct().OrderBy(item => item.Descripcion).ToList();

                foreach (var myEntity in res.ToArray())
                {
                    TD_UBIGEO u = new TD_UBIGEO();
                    u.COD_DEPARTAMENTO = myEntity.Codigo;
                    u.DES_DEPARTAMENTO = myEntity.Descripcion;
                    lista.Add(u);
                }

            }
            catch (Exception)
            {

            }
            return lista;
        }

        public List<TD_UBIGEO> ListarProvincia(string COD_DEPARTAMENTO) 
        {
            List<TD_UBIGEO> lista = new List<TD_UBIGEO>();
            try
            {
                var results = from c in ContextoDeObjs.TD_UBIGEO
                        where (c.COD_DEPARTAMENTO == COD_DEPARTAMENTO)
                        group c by new { c.COD_PROVINCIA, c.DES_PROVINCIA } into p
                        orderby p.Key.DES_PROVINCIA ascending
                              select new
                              {
                                  Codigo = p.Key.COD_PROVINCIA,
                                  Descripcion = p.Key.DES_PROVINCIA
                              };

                var res = results.Distinct().OrderBy(item => item.Descripcion).ToList();

                foreach (var myEntity in res.ToArray())
                {
                    TD_UBIGEO u = new TD_UBIGEO();
                    u.COD_PROVINCIA = myEntity.Codigo;
                    u.DES_PROVINCIA = myEntity.Descripcion;
                    lista.Add(u);
                }

            }
            catch (Exception)
            {

            }
            return lista;
        }

        public List<TD_UBIGEO> ListarDistrito(string COD_PROVINCIA)
        {
            List<TD_UBIGEO> lista = new List<TD_UBIGEO>();
            try
            {
                var results = from c in ContextoDeObjs.TD_UBIGEO
                              where (c.COD_PROVINCIA == COD_PROVINCIA)
                              group c by new { c.ID_UBIGEO, c.DES_DISTRITO } into p
                              orderby p.Key.DES_DISTRITO ascending
                              select new
                              {
                                  Codigo = p.Key.ID_UBIGEO,
                                  Descripcion = p.Key.DES_DISTRITO
                              };

                var res = results.Distinct().OrderBy(item => item.Descripcion).ToList();

                foreach (var myEntity in res.ToArray())
                {
                    TD_UBIGEO u = new TD_UBIGEO();
                    u.ID_UBIGEO = myEntity.Codigo;
                    u.DES_DISTRITO = myEntity.Descripcion;
                    lista.Add(u);
                }
            }
            catch (Exception)
            {

            }
            return lista;
        }

        public TD_UBIGEO Seleccionar(int ID_UBIGEO)
        {
            TD_UBIGEO u = new TD_UBIGEO();
            try
            {
                var results = from c in ContextoDeObjs.TD_UBIGEO
                              where (c.ID_UBIGEO == ID_UBIGEO)
                              select c;

                foreach (var myEntity in results.Distinct().ToArray())
                {
                    u.COD_DEPARTAMENTO = myEntity.COD_DEPARTAMENTO;
                    u.COD_DISTRITO = myEntity.COD_DISTRITO;
                    u.COD_PROVINCIA = myEntity.COD_PROVINCIA;
                }
            }
            catch (Exception)
            {

            }
            return u;
        }
    }
}
